package org.clockworkmages.games.anno1186.gui;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.Timer;

public class AnimatedPanel
		extends JPanel implements ActionListener {
	private Map<String, AnimatedImage> images = new LinkedHashMap<String, AnimatedImage>();

	private Timer timer;

	private long lastPerformed = System.currentTimeMillis();

	public AnimatedPanel() {
		super();
		timer = new Timer(50, this);
		timer.setInitialDelay(100);
		timer.start();
		this.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(final ComponentEvent e) {
				for (AnimatedImage animatedImage : images.values()) {
					// TODO
				}
				AnimatedPanel.this.repaint();
			}
		});
	}

	public Map<String, AnimatedImage> getImages() {
		return images;
	}

	@Override
	public void paintComponent(final Graphics g) {

		long interval = System.currentTimeMillis() - lastPerformed;
		lastPerformed = System.currentTimeMillis();
		for (AnimatedImage animatedImage : images.values()) {
			animatedImage.update(interval);
			animatedImage.draw(g, this);
		}
		super.paintComponent(g);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.repaint();
	}
}
